
#include "BillboardTools.h"

#define bitmapwidth  2048
#define bitmapheight 2048/16

//#define imagesx      16
//#define imagesy      1

//#define channels 4

#define imagewidth    (bitmapwidth/imagesx)
#define imageheight   (bitmapheight/imagesy)

int main() 
{

 	node = smgr->addAnimatedMeshSceneNode( smgr->getMesh("raindrop.obj") );
 	node->setScale(core::vector3df(40,40,40));
	node->setPosition(core::vector3df(0,0,300));
	node->updateAbsolutePosition();
	
	
//	node->setMaterialTexture(0, driver->getTexture("rock.bmp"));
	//driver->makeColorKeyTexture(driver->getTexture("leaf.bmp"),core::position2d<s32>(1,1));
	node->setMaterialFlag(video::EMF_LIGHTING,false);
    
    // width and height of image in memory
    s32 mw=roundup(imagewidth);
    s32 mh=roundup(imageheight);

    
    // create memory for images
    core::array<u8> tempdata;
    core::array<u8> bitmapdata;
    
    tempdata.set_used(mw*mh*Channels);
    bitmapdata.set_used(bitmapwidth*bitmapheight*Channels);

    u8 *pixels = &tempdata[0]; 

    // vector for rotation
    core::vector3df rot(0, 0, 0);
    
    s32 bitmapfullwidth = bitmapwidth*Channels;
    s32 mwfull          = mw*Channels;
    
    // draw the scene (bah!)
    driver->beginScene(true, true, video::SColor(0,255,0,255) );
    smgr->drawAll();
    driver->endScene();

    for (s32 iy=0;iy<imagesy;++iy)
       for (s32 ix=0;ix<imagesx;++ix)
       {
            // rotate the node
            rot.X = (360.0f / imagesx) *ix*-1;          
            node->setRotation(rot);
            
            node->setFrameLoop(iy*20,iy*20);
            node->updateAbsolutePosition();
            device->run();
            
            // set the viewport
            driver->setViewPort( core::rect<s32>(0,0,imagewidth,imageheight) );

            
            // draw the scene
            driver->beginScene(true, true, bgcol );
            smgr->drawAll();
            driver->endScene();

            // copy the scene into temp memory
            glReadBuffer(GL_BACK);
            glReadPixels(0, 0, mw, mh, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)pixels );
            
            // copy data into the large bitmap...
            for (int y = 0; y < imageheight; ++y)
               for (int x = 0; x < imagewidth*Channels; x++)
                 bitmapdata[ (bitmapfullwidth*imageheight*iy) + y*bitmapfullwidth +
                             (imagewidth*Channels*ix +x) ] = pixels[(mh-imageheight+y)*mwfull +x];
            
            
        }
    
    tgaWriter("big.tga", &bitmapdata[0], bitmapwidth, bitmapheight, Channels);
    
    // clear memory
    tempdata.set_used(0);
    bitmapdata.set_used(0);
//std::system("pause");
    return 0;
}
